package com.ruoyi.project.yy.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.domain.SysPost;
import com.ruoyi.project.yy.domain.YyDept;
import com.ruoyi.project.yy.service.YyDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 科室信息操作处理
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/yy/dept")
public class YyDeptController extends BaseController {
    @Autowired
    private YyDeptService deptService;

    /**
     * 获取科室列表
     */
    @PreAuthorize("@ss.hasPermi('yy:dept:list')")
    @GetMapping("/list")
    public TableDataInfo list(YyDept dept) {
        startPage();
        List<YyDept> list = deptService.selectDeptList(dept);
        return getDataTable(list);
    }


    /**
     * 新增岗位
     */
    @PreAuthorize("@ss.hasPermi('yy:dept:add')")
    @Log(title = "科室管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody YyDept dept) {
        return toAjax(deptService.insertDept(dept));
    }


    /**
     * 根据科室编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('yy:dept:query')")
    @GetMapping(value = "/{deptId}")
    public AjaxResult getInfo(@PathVariable Long deptId)
    {
        return AjaxResult.success(deptService.selectDeptById(deptId));
    }

    /**
     * 修改岗位
     */
    @PreAuthorize("@ss.hasPermi('yy:dept:edit')")
    @Log(title = "科室管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody YyDept dept)
    {

        return toAjax(deptService.updateDept(dept));
    }

    /**
     * 删除岗位
     */
    @PreAuthorize("@ss.hasPermi('yy:dept:remove')")
    @Log(title = "科室管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{deptIds}")
    public AjaxResult remove(@PathVariable Long[] deptIds)
    {
        return toAjax(deptService.deleteDeptByIds(deptIds));
    }


    /**
     * 导出科室信息
     * @param dept
     * @return
     */
    @Log(title = "科室管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('yy:dept:export')")
    @GetMapping("/export")
    public AjaxResult export(YyDept dept)
    {
        List<YyDept> list = deptService.selectDeptList(dept);
        ExcelUtil<YyDept> util = new ExcelUtil<YyDept>(YyDept.class);
        return util.exportExcel(list, "科室数据");
    }
}
